1//////////////////////////////////////////////////////////////////////
2// LibFile: wiring.scad
3// Rendering for wiring bundles
4// To use, include the following line at the top of your file:
5// ```
6// use <BOSL/wiring.scad>
7// ```
8//////////////////////////////////////////////////////////////////////
9
10/*
11BSD 2-Clause License
12
13Copyright (c) 2017, Revar Desmera
14All rights reserved.
15
16Redistribution and use in source and binary forms, with or without
17modification, are permitted provided that the following conditions are met:
18
19* Redistributions of source code must retain the above copyright notice, this
20 list of conditions and the following disclaimer.
21
22* Redistributions in binary form must reproduce the above copyright notice,
23 this list of conditions and the following disclaimer in the documentation
24 and/or other materials provided with the distribution.
25
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*/
37
38
39include <math.scad>
40include <paths.scad>
41include <beziers.scad>
42
43
44// Section: Functions
45
46
47// Function: hex_offset_ring()
48// Description:
49// Returns a hexagonal ring of points, with a spacing of `d`.
50// If `lev=0`, returns a single point at `[0,0]`. All greater
51// levels return 6 times `lev` points.
52// Usage:
53// hex_offset_ring(d, lev)
54// Arguments:
55// d = Base unit diameter to build rings upon.
56// lev = How many rings to produce.
57// Example:
58// hex_offset_ring(d=1, lev=3); // Returns a hex ring of 18 points.
59function hex_offset_ring(d, lev=0) =
60 (lev == 0)? [[0,0]] : [
61 for (
62 sideang = [0:60:359.999],
63 sidenum = [1:lev]
64 ) [
65 lev*d*cos(sideang)+sidenum*d*cos(sideang+120),
66 lev*d*sin(sideang)+sidenum*d*sin(sideang+120)
67 ]
68 ];
69
70
71// Function: hex_offsets()
72// Description:
73// Returns the centerpoints for the optimal hexagonal packing
74// of at least `n` circular items, of diameter `d`. Will return
75// enough points to fill out the last ring, even if that is more
76// than `n` points.
77// Usage:
78// hex_offsets(n, d)
79// Arguments:
80// n = Number of items to bundle.
81// d = How far to space each point away from others.
82function hex_offsets(n, d, lev=0, arr=[]) =
83 (len(arr) >= n)? arr :
84 hex_offsets(
85 n=n,
86 d=d,
87 lev=lev+1,
88 arr=concat(arr, hex_offset_ring(d, lev=lev))
89 );
90
91
92
93// Section: Modules
94
95
96// Module: wiring()
97// Description:
98// Returns a 3D object representing a bundle of wires that follow a given path,
99// with the corners filleted to a given radius. There are 17 base wire colors.
100// If you have more than 17 wires, colors will get re-used.
101// Usage:
102// wiring(path, wires, [wirediam], [fillet], [wirenum], [bezsteps]);
103// Arguments:
104// path = The 3D polyline path that the wire bundle should follow.
105// wires = The number of wires in the wiring bundle.
106// wirediam = The diameter of each wire in the bundle.
107// fillet = The radius that the path corners will be filleted to.
108// wirenum = The first wire's offset into the color table.
109// bezsteps = The corner fillets in the path will be converted into this number of segments.
110// Example:
111// wiring([[50,0,-50], [50,50,-50], [0,50,-50], [0,0,-50], [0,0,0]], fillet=10, wires=13);
112module wiring(path, wires, wirediam=2, fillet=10, wirenum=0, bezsteps=12) {
113 colors = [
114 [0.2, 0.2, 0.2], [1.0, 0.2, 0.2], [0.0, 0.8, 0.0], [1.0, 1.0, 0.2],
115 [0.3, 0.3, 1.0], [1.0, 1.0, 1.0], [0.7, 0.5, 0.0], [0.5, 0.5, 0.5],
116 [0.2, 0.9, 0.9], [0.8, 0.0, 0.8], [0.0, 0.6, 0.6], [1.0, 0.7, 0.7],
117 [1.0, 0.5, 1.0], [0.5, 0.6, 0.0], [1.0, 0.7, 0.0], [0.7, 1.0, 0.5],
118 [0.6, 0.6, 1.0],
119 ];
120 offsets = hex_offsets(wires, wirediam);
121 bezpath = fillet_path(path, fillet);
122 poly = simplify3d_path(path3d(bezier_polyline(bezpath, bezsteps)));
123 n = max(segs(wirediam), 8);
124 r = wirediam/2;
125 for (i = [0:wires-1]) {
126 extpath = [for (j = [0:n-1]) let(a=j*360/n) [r*cos(a)+offsets[i][0], r*sin(a)+offsets[i][1]]];
127 color(colors[(i+wirenum)%len(colors)]) {
128 extrude_2dpath_along_3dpath(extpath, poly);
129 }
130 }
131}
132
133
134
135// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap